package com.lanhai.zhonglai.third;

import java.util.Arrays;
import java.util.List;

import com.lanhai.zhonglai.beans.StationDataBean;
import com.lanhai.zhonglai.beans.StationMountBean;
import com.lanhai.zhonglai.enums.StationDataTypeEnum;
import com.lanhai.zhonglai.enums.TransDirectionEnum;
import com.lanhai.zhonglai.interfaces.IStationProtocol;
import com.lanhai.zhonglai.utils.Tools;

/**
 * ALD
 * @author zhengwei
 *
 */
public class ALD  implements IStationProtocol{
	@Override
	public byte[] agvArrived(byte line, TransDirectionEnum type) {
		byte[] buf = new byte[8];
		buf[0] = line;
		buf[2] = 1;
		buf[4] = 1;
		return buf;
	}

	@Override
	public byte[] applyLeave(byte line, TransDirectionEnum type) {
		byte[] buf = new byte[8];
		buf[0] = line;
		buf[2] = 2;
		buf[4] = 2;
		return buf;
	}

	@Override
	public List<StationDataBean> analyseData(String ip, byte[] buf) {
		StationDataBean obj = new StationDataBean();
		StationDataBean obj2 = new StationDataBean();
		obj.setLine(buf[0]);  //真实线别，指令使用
		obj.setCmdStr(Tools.byte2HexStr(buf));
		obj.setIp(ip);

		obj2.setLine((byte)2);
		obj2.setCmdStr(Tools.byte2HexStr(buf));
		obj2.setIp(ip);
		
		if (buf[2] == 4) {
			// 机台异常
			obj.setEventType(StationDataTypeEnum.Error);
			return Arrays.asList(obj);
		}
		if (buf[2] == 3 || buf[4] == 3) {
			// 允许离开
			obj.setEventType(StationDataTypeEnum.AllowLeave);
			return Arrays.asList(obj);
		}
		if (buf[2] == 2 || buf[4] == 2) {
			// 允许传输（只返回一条）
			obj.setEventType(StationDataTypeEnum.AllowTrans);
			StationMountBean mbx = new StationMountBean(); 
			int income = buf[6];
			int outnum = buf[7];
			if(buf[0]==2) {
				//2线的
				 income = buf[8];
			     outnum = buf[9];
			} 
			mbx.setInNum(income);
			if (income == 0) {
				mbx.setAllowIn(true);
			}
			// 出料
			
			mbx.setOutNum(outnum);
			if (outnum >= 10) {
				mbx.setAllowOut(true);
			}
			obj.setBean(mbx);
			 
			return Arrays.asList(obj);
		}
		if (buf[2] == 5 || buf[4] == 5) {
			// 允许转动
			obj.setEventType(StationDataTypeEnum.Receive);
			return Arrays.asList(obj);
		}
		//
		obj.setEventType(StationDataTypeEnum.Info); 
		obj2.setEventType(StationDataTypeEnum.Info); 
		
		//02 00 03 00 03 00   00 10 00 00  
		//线别1 第一个字
		StationMountBean mb1 = new StationMountBean(); 
		int income = buf[6];
		mb1.setInNum(income);
		if (income == 0) {
			mb1.setAllowIn(true);
		}
		// 出料
		int outnum = buf[7];
		mb1.setOutNum(outnum);
		if (outnum >= 10) {
			mb1.setAllowOut(true);
		}
		obj.setBean(mb1);
		
		//线别2 第二个字
		StationMountBean mb2 = new StationMountBean();
		income = buf[8];
		mb2.setInNum(income);
		if (income == 0) {
			mb2.setAllowIn(true);
		}
		// 出料
		outnum = buf[9];
		mb2.setOutNum(outnum);
		if (outnum >= 10) {
			mb2.setAllowOut(true);
		}
		obj2.setBean(mb2);
		
		return Arrays.asList(obj,obj2);
	}

	@Override
	public byte[] agvLeave(byte line, TransDirectionEnum type) {
		byte[] buf = new byte[8];
		buf[0] = line;
		buf[2] = 0;
		buf[4] = 0;
		return buf;
	}

	@Override
	public byte[] applyRecieve(byte line, TransDirectionEnum type) {
		byte[] buf = new byte[8];
		buf[0] = line;
		switch (type) {
		case Both:
			buf[2] = 3;
			buf[4] = 3;
			break;
		case OnlyIn:
			buf[2] = 3;
			buf[4] = 0;
			break;
		case OnlyOut:
			buf[2] = 0;
			buf[4] = 3;
			break;
		}
		return buf;
	}

	@Override
	public byte[] transError(byte line, TransDirectionEnum type) {
		byte[] buf = new byte[8];
		buf[0] = line;
		buf[2] = 4;
		buf[4] = 4;
		return buf;
	}

}
